import Vue from 'vue'
import App from './App.vue'
import router from './router'

Vue.config.productionTip = false

// 路由拦截
router.beforeEach((to, from, next) => {
  if (to.meta.needAuth) {
    if (localStorage.getItem("token")) {
      next()
    } else {
      router.push('/login')
    }
  } else {
    // 放行
    next()
  }
})


// 请求拦截器
axios.interceptors.request.use(config => {
  // 条件判断,是否需要带上token
  // 要么已经登录(有token) 要么请求代码本身没有带上 Authorization
  if (localStorage.getItem('token') && !config.headers.Authorization)
    config.headers.Authorization = "Bearer " + localStorage.getItem("token")
  // 拦截放行
  return config
})

// 引入axios
import axios from "axios"
// 绑定到原型
Vue.prototype.$axios = axios;

// 判断本地路径线上路径
if (process.env.NODE_ENV == "development") {
  // 设置默认的api 域名基准路径
  axios.defaults.baseURL = 'http://127.0.0.1:3000'
} else {
  axios.defaults.baseURL = "http://157.122.54.189:9083"
}


// 引入ui库
import ElementUI from "element-ui"
// 引入css
import "element-ui/lib/theme-chalk/index.css"

// 注册组件库
Vue.use(ElementUI)


new Vue({
  router,
  render: function (h) {
    return h(App)
  }
}).$mount('#app')